//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "JPYLibor.h"
using namespace Cephei::QL::Indexes::Ibor;
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/YieldTermStructure.h>
#include <gen/QL/Indexes/IborIndex.h>
#include <gen/QL/Currency.h>
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Indexes/Ibor/Libor.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL::Termstructures;
using namespace Cephei::QL::Indexes;
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (Cephei::QL::Times::IPeriod^ tenor, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h) : CLibor(CJPYLibor::typeid)
{
    CPeriod^ _Ctenor;
    CYieldTermStructure^ _Ch;
    try
    {
#ifdef HANDLE
        _phJPYLibor = NULL;
#endif
        _Ctenor = safe_cast<CPeriod^> (tenor);
        _Ctenor->Lock();
        QuantLib::Period& _tenor = static_cast<QuantLib::Period&> (_Ctenor->GetReference ()); 
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h))
        {
            _Ch = safe_cast<CYieldTermStructure^> (h->Value);
            _Ch->Lock();
        }
        Handle<QuantLib::YieldTermStructure>& _h = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>::IsSome::get (h) ? static_cast<Handle<QuantLib::YieldTermStructure>&> (_Ch->GetHandle ()) : Handle<QuantLib::YieldTermStructure>()); //1
        _ppJPYLibor = new boost::shared_ptr<QuantLib::JPYLibor> (new QuantLib::JPYLibor ( _tenor,  _h ));
        SetLibor (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ctenor != nullptr) _Ctenor->Unlock();
        if (_Ch != nullptr) _Ch->Unlock();
    }
}
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (boost::shared_ptr<QuantLib::JPYLibor>& childNative, Object^ owner) : CLibor(CJPYLibor::typeid)
{
#ifdef HANDLE
	_phJPYLibor = NULL;
#endif
	_ppJPYLibor = &childNative;
    _ppLibor = new boost::shared_ptr<QuantLib::Libor> (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
}
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (QuantLib::JPYLibor& childNative, Object^ owner) : CLibor(CJPYLibor::typeid)
{
#ifdef HANDLE
	_phJPYLibor = NULL;
#endif
	_ppJPYLibor = new boost::shared_ptr<QuantLib::JPYLibor> (&childNative);
    _ppLibor = new boost::shared_ptr<QuantLib::Libor> (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
    _JPYLiborOwner = owner;
    _LiborOwner = owner;
}

Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (CJPYLibor^ copy) : CLibor(CJPYLibor::typeid)
{
#ifdef HANDLE
	_phJPYLibor = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppJPYLibor = new boost::shared_ptr<QuantLib::JPYLibor> (copy->GetShared());
        _ppLibor = new boost::shared_ptr<QuantLib::Libor> (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
    }
}
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (PLATFORM::Type^ t) : CLibor(CJPYLibor::typeid)
{
#ifdef HANDLE
	_phJPYLibor = NULL;
#endif
	if (!t->IsSubclassOf(CJPYLibor::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (QuantLib::Handle<QuantLib::JPYLibor>& childNative, Object^ owner)  : CLibor(CJPYLibor::typeid)
{
	_phJPYLibor = &childNative;
	_ppJPYLibor = &static_cast<boost::shared_ptr<QuantLib::JPYLibor>>(childNative.currentLink());
    _ppLibor = new boost::shared_ptr<QuantLib::Libor> (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
    _JPYLiborOwner = owner;
}
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (QuantLib::Handle<QuantLib::JPYLibor> childNative)  : CLibor(CJPYLibor::typeid)
{
	_phJPYLibor = &childNative;
	_ppJPYLibor = &static_cast<boost::shared_ptr<QuantLib::JPYLibor>>(childNative.currentLink());
    _ppLibor = new boost::shared_ptr<QuantLib::Libor> (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
}
#endif
#ifdef STRUCT
Cephei::QL::Indexes::Ibor::CJPYLibor::CJPYLibor (QuantLib::JPYLibor childNative)  : CLibor(CJPYLibor::typeid)
{
#ifdef HANDLE
	_phJPYLibor = NULL;
#endif
	_ppJPYLibor = new boost::shared_ptr<QuantLib::JPYLibor> (new QuantLib::JPYLibor (childNative));
    _ppLibor = new boost::shared_ptr<QuantLib::Libor> (boost::dynamic_pointer_cast<QuantLib::Libor> (*_ppJPYLibor));
}
#endif

Cephei::QL::Indexes::Ibor::CJPYLibor::~CJPYLibor ()
{
    if (_ppJPYLibor != NULL)
    {
	    delete _ppJPYLibor;
        _ppJPYLibor = NULL;
    }
}
Cephei::QL::Indexes::Ibor::CJPYLibor::!CJPYLibor ()
{
    if (_ppJPYLibor != NULL)
    {
	    delete _ppJPYLibor;
    }
}
QuantLib::JPYLibor& Cephei::QL::Indexes::Ibor::CJPYLibor::GetReference ()
{
    if (_ppJPYLibor == NULL) throw REFNEW NativeNullException ();
	return **_ppJPYLibor;
}
boost::shared_ptr<QuantLib::JPYLibor>& Cephei::QL::Indexes::Ibor::CJPYLibor::GetShared ()
{
    if (_ppJPYLibor == NULL) throw REFNEW NativeNullException ();
	return *_ppJPYLibor;
}
QuantLib::JPYLibor* Cephei::QL::Indexes::Ibor::CJPYLibor::GetPointer ()
{
    if (_ppJPYLibor == NULL) throw REFNEW NativeNullException ();
	return &**_ppJPYLibor;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::JPYLibor>& Cephei::QL::Indexes::Ibor::CJPYLibor::GetHandle ()
{
	if (_phJPYLibor == NULL)
	{
		_phJPYLibor = new Handle<QuantLib::JPYLibor> (*_ppJPYLibor);
	}
	return *_phJPYLibor;
}
#endif
bool Cephei::QL::Indexes::Ibor::CJPYLibor::HasNative () 
{
	return (_ppJPYLibor != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Indexes::Ibor::IJPYLibor^ Cephei::QL::Indexes::Ibor::CJPYLibor_Factory::Create (Cephei::QL::Times::IPeriod^ tenor, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Termstructures::IYieldTermStructure^>^ h)
{
    return REFNEW CJPYLibor ( tenor,  h);
}
